using System.Text.Json;
using Mathmall.Administration.common;
using Mathmall.Administration.Mapper;
using Mathmall.Administration.Models;
using Mathmall.Administration.Vo;

namespace Mathmall.Administration.Service.impl;

public class AttrGroupService : IAttrGroupService
{
    private readonly AttrGroupMapper _attrGroupMapper;
    private readonly ICategoryService _categoryService;

    public AttrGroupService(AttrGroupMapper attrGroupMapper, ICategoryService categoryService)
    {
        _attrGroupMapper = attrGroupMapper;
        _categoryService = categoryService;
    }

    /// <summary>
    /// 新增属性分组
    /// </summary>
    /// <param name="attrGroupVo"></param>
    /// <returns></returns>
    public async Task<int> AddAttrGroup(AttrGroupVo attrGroupVo)
    {
        AttrGroup attrGroup = JsonSerializer.Deserialize<AttrGroup>(JsonSerializer.Serialize(attrGroupVo))!;
        return await _attrGroupMapper.CreateAttrGroup(attrGroup);
    }

    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <param name="condition">模糊查询条件</param>
    /// <param name="categoryId">分类id</param>
    /// <returns></returns>
    public async Task<PaginatedList<AttrGroupVo>> PaginatedQuery(int pageIndex, int pageSize, string condition,
        int categoryId)
    {
        // 查询全部符合条件的数据
        IQueryable<AttrGroupVo> attrGroupVos = _attrGroupMapper.GetAttrGroupVos()
            .OrderBy(attrGroupVo => attrGroupVo.Sort)
            .Where(attrGroupVo =>
                (attrGroupVo.AttrGroupName.Contains(condition) || attrGroupVo.Description.Contains(condition)) &&
                (categoryId == 0 ? attrGroupVo.CatalogID > categoryId : attrGroupVo.CatalogID == categoryId));

        // 分页处理
        PaginatedList<AttrGroupVo> attrGroupsPaginatedList =
            await PaginatedList<AttrGroupVo>.CreateAsync(attrGroupVos, pageIndex, pageSize);

        return attrGroupsPaginatedList;
    }

    /// <summary>
    /// 根据id查询属性分组
    /// </summary>
    /// <param name="attrGroupID"></param>
    /// <returns></returns>
    public async Task<AttrGroupVo> GetAttrGroupById(int attrGroupID)
    {
        // 查询属性分组信息
        AttrGroup attrGroup = await _attrGroupMapper.GetAttrGroupById(attrGroupID);

        // 查询属性分组的id路径
        List<int> categoryIdPath = await _categoryService.GetCategoryIdPath(attrGroup.CatalogID);
        categoryIdPath.Reverse();
        AttrGroupVo attrGroupVo = JsonSerializer.Deserialize<AttrGroupVo>(JsonSerializer.Serialize(attrGroup))!;
        attrGroupVo.IdPath = categoryIdPath;

        return attrGroupVo;
    }

    /// <summary>
    /// 修改属性分组
    /// </summary>
    /// <param name="attrGroupVo"></param>
    /// <returns></returns>
    public async Task<int> UpdateAttrGroup(AttrGroupVo attrGroupVo)
    {
        AttrGroup attrGroup = JsonSerializer.Deserialize<AttrGroup>(JsonSerializer.Serialize(attrGroupVo))!;
        return await _attrGroupMapper.UpdateAttrGroup(attrGroup);
    }

    /// <summary>
    /// 删除属性分组
    /// </summary>
    /// <param name="attrGroupIDs"></param>
    /// <returns></returns>
    public async Task<int> DeleteAttrGroup(List<int> attrGroupIDs)
    {
        return await _attrGroupMapper.DeleteAttrGroup(attrGroupIDs);
    }

    /// <summary>
    /// 根据分类id查询属性分组
    /// </summary>
    /// <param name="catalogId"></param>
    /// <returns></returns>
    public async Task<List<AttrGroupVo>> GetAttrGroupsByCatalogID(int catalogId)
    {
        List<AttrGroup> attrGroups = await _attrGroupMapper.GetAttrGroupsByCatalogId(catalogId);
        List<AttrGroupVo> attrGroupVos =
            JsonSerializer.Deserialize<List<AttrGroupVo>>(JsonSerializer.Serialize(attrGroups))!;
        return attrGroupVos;
    }

    /// <summary>
    /// 根据属性id查询分组信息
    /// </summary>
    /// <param name="attrID"></param>
    /// <returns></returns>
    public async Task<AttrGroupVo> GetAttrGroupByAttrId(int attrID)
    {
        AttrGroup attrGroup = await _attrGroupMapper.GetAttrGroupByAttrId(attrID);
        AttrGroupVo attrGroupVo = JsonSerializer.Deserialize<AttrGroupVo>(JsonSerializer.Serialize(attrGroup))!;
        return attrGroupVo;
    }
}